<?php
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
if (isset($_GET['echostr'])) {
    $wechatObj->valid();
}else{
    $wechatObj->responseMsg();
}

class wechatCallbackapiTest
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
            header('content-type:text');
            echo $echoStr;
            exit;
        }
    }

    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }

    public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

        if (!empty($postStr)){
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $fromUsername = $postObj->FromUserName;
            $toUsername = $postObj->ToUserName;
            $keyword = trim($postObj->Content);
            $ev = $postObj->Event;
            $time = time();
            $textTpl = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[%s]]></MsgType>
                        <Content><![CDATA[%s]]></Content>
                        <FuncFlag>0</FuncFlag>
                        </xml>";
            
            
            if ($ev == "subscribe")
            {
  				$msgType = "text";
  				$contentStr = "Weclcome to use the Chome
You can choose the option to select 
1.inter o1/s1 open/shut yard ylght
2.inter oo/ss open/shut all lights
3.inter o3/s3 open/close door light
4.inter co/cs open/close your carport
5.inter BF/SBF open/close your protect
6.inter temp to get tempereture
";
  				$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
  				echo $resultStr;
				}
            
            //=======================================change state庭院灯1========================================
			 if($keyword =="o1")
            {
                $msgType = "text";
               
                 //************数据库连接修改操作************* 
                 $con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS); 
  		         $dati = date("h:i:sa");
  	        	 mysql_select_db("app_cchome", $con);//修改数据库名
				 $sql ="UPDATE switch SET timestamp='$dati',state = '1'WHERE ID = '1'";//修改开关状态值
				if(!mysql_query($sql,$con))
                {
            		die('Error: ' . mysql_error());
        		}else
                {
                mysql_close($con);
                $contentStr = "success open light1 ";
        		}        
				//*******************************************
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                echo $resultStr;
            }
            //==================================================================================
            
             
            //=======================================change state庭院灯========================================
			 if($keyword =="s1")
            {
                $msgType = "text";
               
                 //************数据库连接修改操作************* 
                 $con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS); 
  		         $dati = date("h:i:sa");
  	        	 mysql_select_db("app_cchome", $con);//修改数据库名
				 $sql ="UPDATE switch SET timestamp='$dati',state = '2'WHERE ID = '1'";//修改开关状态值
				if(!mysql_query($sql,$con))
                {
            		die('Error: ' . mysql_error());
        		}else
                {
                mysql_close($con);
                $contentStr = "success shut light1 ";
        		}        
				//*******************************************
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                echo $resultStr;
            }
            //=================================================================================

            
            //=======================================change state开启所有灯========================================
			 if($keyword =="oo")
            {
                $msgType = "text";
               
                 //************数据库连接修改操作************* 
                 $con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS); 
  		         $dati = date("h:i:sa");
  	        	  
                 mysql_select_db("app_cchome", $con);//修改数据库名
				 $sql ="UPDATE switch SET timestamp='$dati',state = '1'WHERE ID = '1'";//修改开关状态值
                 if(!mysql_query($sql,$con))
                {
            		die('Error: ' . mysql_error());
        		}else
                {
                mysql_close($con);
                $contentStr = "success open lights ";
        		}        
				//*******************************************
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                echo $resultStr;
                 
                 
                   //************数据库连接修改操作************* 
                 $con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS); 
  		         $dati = date("h:i:sa");
  	        	  
                 mysql_select_db("app_cchome", $con);//修改数据库名
				 $sql ="UPDATE switch SET timestamp='$dati',state = '1'WHERE ID = '2'";//修改开关状态值
                 if(!mysql_query($sql,$con))
                {
            		die('Error: ' . mysql_error());
        		}else
                {
                mysql_close($con);
                $contentStr = "success open lights ";
        		}        
				//*******************************************
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                echo $resultStr;
                 
                 
                   //************数据库连接修改操作************* 
                 $con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS); 
  		         $dati = date("h:i:sa");
  	        	  
                 mysql_select_db("app_cchome", $con);//修改数据库名
				 $sql ="UPDATE switch SET timestamp='$dati',state = '1'WHERE ID = '3'";//修改开关状态值
                 if(!mysql_query($sql,$con))
                {
            		die('Error: ' . mysql_error());
        		}else
                {
                mysql_close($con);
                $contentStr = "success open lights ";
        		}        
				//*******************************************
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                echo $resultStr;
            }
            //==================================================================================
            
            //=======================================change state关闭所有灯========================================
			 if($keyword =="ss")
            {
                $msgType = "text";
               
                 //************数据库连接修改操作************* 
                 $con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS); 
  		         $dati = date("h:i:sa");
  	        	 mysql_select_db("app_cchome", $con);//修改数据库名
				 $sql ="UPDATE switch SET timestamp='$dati',state = '2'WHERE ID = '1'";//修改开关状态值、
                 
				if(!mysql_query($sql,$con))
                {
            		die('Error: ' . mysql_error());
        		}else
                {
                mysql_close($con);
                $contentStr = "success close lights ";
        		}        
				//*******************************************
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                echo $resultStr;
                 
                 
                 //************数据库连接修改操作************* 
                 $con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS); 
  		         $dati = date("h:i:sa");
  	        	 mysql_select_db("app_cchome", $con);//修改数据库名
				 $sql ="UPDATE switch SET timestamp='$dati',state = '2'WHERE ID = '2'";//修改开关状态值、
                 
				if(!mysql_query($sql,$con))
                {
            		die('Error: ' . mysql_error());
        		}else
                {
                mysql_close($con);
                $contentStr = "success close lights ";
        		}        
				//*******************************************
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                echo $resultStr;
                 
                 
                 //************数据库连接修改操作************* 
                 $con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS); 
  		         $dati = date("h:i:sa");
  	        	 mysql_select_db("app_cchome", $con);//修改数据库名
				 $sql ="UPDATE switch SET timestamp='$dati',state = '2'WHERE ID = '3'";//修改开关状态值、
                 
				if(!mysql_query($sql,$con))
                {
            		die('Error: ' . mysql_error());
        		}else
                {
                mysql_close($con);
                $contentStr = "success close lights ";
        		}        
				//*******************************************
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                echo $resultStr;
            }
            //=================================================================================
            
             //=======================================change state开启门灯========================================
			 if($keyword =="o3")
            {
                $msgType = "text";
               
                 //************数据库连接修改操作************* 
                 $con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS); 
  		         $dati = date("h:i:sa");
  	        	 mysql_select_db("app_cchome", $con);//修改数据库名
				 $sql ="UPDATE switch SET timestamp='$dati',state = '1'WHERE ID = '3'";//修改开关状态值
				if(!mysql_query($sql,$con))
                {
            		die('Error: ' . mysql_error());
        		}else
                {
                mysql_close($con);
                $contentStr = "success open light3 ";
        		}        
				//*******************************************
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                echo $resultStr;
            }
            //==================================================================================
            
            //=======================================change state关闭门灯========================================
			 if($keyword =="s3")
            {
                $msgType = "text";
               
                 //************数据库连接修改操作************* 
                 $con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS); 
  		         $dati = date("h:i:sa");
  	        	 mysql_select_db("app_cchome", $con);//修改数据库名
				 $sql ="UPDATE switch SET timestamp='$dati',state = '2'WHERE ID = '3'";//修改开关状态值
				if(!mysql_query($sql,$con))
                {
            		die('Error: ' . mysql_error());
        		}else
                {
                mysql_close($con);
                $contentStr = "success close light3 ";
        		}        
				//*******************************************
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                echo $resultStr;
            }
            //=================================================================================
            
              //=======================================car port open========================================
			 if($keyword =="co")
            {
                $msgType = "text";
               
                 //************数据库连接修改操作************* 
                 $con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS); 
  		         $dati = date("h:i:sa");
  	        	 mysql_select_db("app_cchome", $con);//修改数据库名
				 $sql ="UPDATE switch SET timestamp='$dati',state = '1'WHERE ID = '4'";//修改开关状态值
				if(!mysql_query($sql,$con))
                {
            		die('Error: ' . mysql_error());
        		}else
                {
                mysql_close($con);
                $contentStr = " you are welcom success open ";
        		}        
				//*******************************************
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                echo $resultStr;
            }
            //==================================================================================
            
            //=======================================car port close========================================
			 if($keyword =="cs")
            {
                $msgType = "text";
               
                 //************数据库连接修改操作************* 
                 $con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS); 
  		         $dati = date("h:i:sa");
  	        	 mysql_select_db("app_cchome", $con);//修改数据库名
				 $sql ="UPDATE switch SET timestamp='$dati',state = '2'WHERE ID = '4'";//修改开关状态值
				if(!mysql_query($sql,$con))
                {
            		die('Error: ' . mysql_error());
        		}else
                {
                mysql_close($con);
                $contentStr = "success close car port ";
        		}        
				//*******************************************
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                echo $resultStr;
            }
            //=================================================================================
            
            
            
             //=======================================开启防盗模块========================================
            //布防控制模块，所有灯光全部关闭，红外开启
            if($keyword =="BF"||$keyword =="布防")
            {
               $msgType = "text";
               
                 //************数据库连接修改操作************* 
                 $con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS); 
  		         $dati = date("h:i:sa");
  	        	 mysql_select_db("app_cchome", $con);//修改数据库名
				 $sql ="UPDATE switch SET timestamp='$dati',state = '1'WHERE ID = '5'";//修改开关状态值
				if(!mysql_query($sql,$con))
                {
            		die('Error: ' . mysql_error());
        		}else
                {
                mysql_close($con);
                $contentStr = " protecting running ";
        		}        
				//*******************************************
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                echo $resultStr;
            }
            //========================================关闭防盗模块=========================================
            
             if($keyword =="SBF"||$keyword =="布防停止")
            {
               $msgType = "text";
               
                 //************数据库连接修改操作************* 
                 $con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS); 
  		         $dati = date("h:i:sa");
  	        	 mysql_select_db("app_cchome", $con);//修改数据库名
				 $sql ="UPDATE switch SET timestamp='$dati',state = '2'WHERE ID = '5'";//修改开关状态值
				if(!mysql_query($sql,$con))
                {
            		die('Error: ' . mysql_error());
        		}else
                {
                mysql_close($con);
                $contentStr = " protecting stop ";
        		}        
				//*******************************************
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                echo $resultStr;
            }
            
            //===========================================================================================
             
            
            
            
            
            
            
            
            
            
            
            
            if($keyword == "time" || $keyword == "time")
            {
                $msgType = "text";
                $contentStr = date("Y-m-d H:i:s",time());
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                echo $resultStr;
            }
        	}
        	else
        	{
            echo "sorry";
            exit;
        	}
        
        
        
    }
}
?>